/*
 * Copyright (c) 2025 CAX Conversion Project
 */
package com.comac.ins.isfd.service.impl.section;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.comac.ins.common.core.exception.base.BaseException;
import com.comac.ins.common.core.utils.MapstructUtils;
import com.comac.ins.common.core.utils.StringUtils;
import com.comac.ins.common.encrypt.utils.EncryptUtils;
import com.comac.ins.isfd.constant.enums.IsfdSectionTypeEnum;
import com.comac.ins.isfd.domain.IsfdSection;
import com.comac.ins.isfd.domain.IsfdSectionResult;
import com.comac.ins.isfd.domain.bo.IsfdSectionBo;
import com.comac.ins.isfd.domain.vo.IsfdLayupEquivalentMaterialVo;
import com.comac.ins.isfd.util.SectionCalculateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 型材库 I型结构 实现类
 *
 * @author hxloongs
 * @date 2024-7-30
 */
@Service
public class IsfdSectionIServiceImpl extends IsfdAbstractSectionService {
    @Override
    public String selectByType() {
        return IsfdSectionTypeEnum.I.getValue();
    }

    @Override
    public double[] calculate(IsfdSectionBo isfdSectionBo) {

        // 查询铺层库代码, 填充W1, H, W2, T1, T2, T3, E1, E2, E3, 	G1, G2, G3
        String t1LayerCode = isfdSectionBo.getT1LayerCode();
        String t2LayerCode = isfdSectionBo.getT2LayerCode();
        String t3LayerCode = isfdSectionBo.getT3LayerCode();
        IsfdLayupEquivalentMaterialVo t1IsfdLayupEquivalentMaterialVo = iIsfdLayupEquivalentMaterialService.queryByLayCode(t1LayerCode);
        IsfdLayupEquivalentMaterialVo t2IsfdLayupEquivalentMaterialVo = iIsfdLayupEquivalentMaterialService.queryByLayCode(t2LayerCode);
        IsfdLayupEquivalentMaterialVo t3IsfdLayupEquivalentMaterialVo = iIsfdLayupEquivalentMaterialService.queryByLayCode(t3LayerCode);

        double[][] layerParametersI ={
            {t1IsfdLayupEquivalentMaterialVo.getT(), t1IsfdLayupEquivalentMaterialVo.getExx(), t1IsfdLayupEquivalentMaterialVo.getGxy(), t1IsfdLayupEquivalentMaterialVo.getRho(), t1IsfdLayupEquivalentMaterialVo.getX()},
            {t2IsfdLayupEquivalentMaterialVo.getT(), t2IsfdLayupEquivalentMaterialVo.getExx(), t2IsfdLayupEquivalentMaterialVo.getGxy(), t2IsfdLayupEquivalentMaterialVo.getRho(), t2IsfdLayupEquivalentMaterialVo.getX()},
            {t3IsfdLayupEquivalentMaterialVo.getT(), t3IsfdLayupEquivalentMaterialVo.getExx(), t3IsfdLayupEquivalentMaterialVo.getGxy(), t3IsfdLayupEquivalentMaterialVo.getRho(), t3IsfdLayupEquivalentMaterialVo.getX()},
        }; // t, Exx, Gxy, Rho, ax
        double[] shapeParametersI ={isfdSectionBo.getW1(), isfdSectionBo.getH(), isfdSectionBo.getW2()}; // W1, H, W2
        double[] sectionPropertiesI = new double[8]; // E_E, E_G, XC, A_ALL, I_ALL, I_ADD, E_RHO, E_THM
        SectionCalculateUtils.calculateIProfile(layerParametersI, shapeParametersI, sectionPropertiesI);

        return sectionPropertiesI;
    }

    @Transactional
    @Override
    public Boolean save(IsfdSectionBo bo) {
        // 1. 参数校验
        checkParams(bo);

        // 2. 获取hash字段
        String hashUniqueKey = EncryptUtils.hashFields(bo.getType(), bo.getSerialNumber(), bo.getAttributeNumber(), bo.getMat1Id(),
            bo.getW1(), bo.getH(), bo.getW2(), bo.getT1LayerCode(), bo.getT2LayerCode(), bo.getT3LayerCode());

        // 3. 幂等校验
        Boolean isUpdate = super.checkUnique(bo, hashUniqueKey);

        // 4. 计算属性
        double[] sectionPropertiesI = this.calculate(bo);

        // 5. 落表
        IsfdSection isfdSection = MapstructUtils.convert(bo, IsfdSection.class);
        isfdSection.setHashUniqueKey(hashUniqueKey);
        isfdSection.setRho(sectionPropertiesI[6]);
        isfdSection.setThm(sectionPropertiesI[7]);
        // 判断是新增业务还是修改业务，处理落表的方式不同
        if (isUpdate) {
            isfdSectionMapper.updateById(isfdSection);
            IsfdSectionResult isfdSectionResult = isfdSectionResultMapper.selectOne(new LambdaQueryWrapper<IsfdSectionResult>()
                .eq(IsfdSectionResult::getSecId, isfdSection.getId()));
            if (isfdSectionResult == null) {
                throw new BaseException("修改时，发现对应结果记录不存在，请确认后重试！");
            }
            isfdSectionResult.setE(sectionPropertiesI[0]);
            isfdSectionResult.setG(sectionPropertiesI[1]);
            isfdSectionResult.setXc(sectionPropertiesI[2]);
            isfdSectionResult.setA(sectionPropertiesI[3]);
            isfdSectionResult.setI1(sectionPropertiesI[4]);
            isfdSectionResult.setI2(sectionPropertiesI[5]);
            isfdSectionResultMapper.updateById(isfdSectionResult);
        }   else {
            isfdSectionMapper.insert(isfdSection);
            IsfdSectionResult isfdSectionResult = new IsfdSectionResult();
            isfdSectionResult.setSecId(isfdSection.getId());
            isfdSectionResult.setE(sectionPropertiesI[0]);
            isfdSectionResult.setG(sectionPropertiesI[1]);
            isfdSectionResult.setXc(sectionPropertiesI[2]);
            isfdSectionResult.setA(sectionPropertiesI[3]);
            isfdSectionResult.setI1(sectionPropertiesI[4]);
            isfdSectionResult.setI2(sectionPropertiesI[5]);
            isfdSectionResultMapper.insert(isfdSectionResult);
        }

        return Boolean.TRUE;
    }


    private void checkParams(IsfdSectionBo isfdSectionBo) {
        if (StringUtils.isBlank(isfdSectionBo.getSerialNumber()) || !isfdSectionBo.getSerialNumber().startsWith(IsfdSectionTypeEnum.I.getValue())) {
            throw new BaseException("编号不能为空或者格式不正确（需要I开头），请确认后重试！");
        }
        if (StringUtils.isBlank(isfdSectionBo.getAttributeNumber())){
            throw new BaseException("属性号不能为空，请确认后重试！");
        }
        if (StringUtils.isBlank(isfdSectionBo.getMat1Id())){
            throw new BaseException("材料属性不能为空，请确认后重试！");
        }
        if (isfdSectionBo.getW1() == null) {
            throw new BaseException("w1不能为空，请确认后重试！");
        }
        if (isfdSectionBo.getH() == null) {
            throw new BaseException("h不能为空，请确认后重试！");
        }
        if (isfdSectionBo.getW2() == null) {
            throw new BaseException("w2不能为空，请确认后重试！");
        }
        if (StringUtils.isBlank(isfdSectionBo.getT1LayerCode())){
            throw new BaseException("t1铺层代号不能为空，请确认后重试！");
        }
        if (StringUtils.isBlank(isfdSectionBo.getT2LayerCode())){
            throw new BaseException("t2铺层代号不能为空，请确认后重试！");
        }
        if (StringUtils.isBlank(isfdSectionBo.getT3LayerCode())){
            throw new BaseException("t3铺层代号不能为空，请确认后重试！");
        }
    }

}
